package com.situ.personal.controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.situ.personal.dao.DepartmentDao;
import com.situ.personal.dao.UserDao;
import com.situ.personal.entity.Department;
import com.situ.personal.entity.PageInfo;
import com.situ.personal.entity.User;
import com.situ.personal.util.JDBCUtil;
import com.situ.personal.vo.UserDeptVO;

@WebServlet("/user")
public class UserServlet extends HttpServlet{
	private UserDao userDao = new UserDao();
	private DepartmentDao departmentDao = new DepartmentDao();

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// http://localhost:8080/Java2012WebPersonal/user?method=selectAll
		req.setCharacterEncoding("UTF-8");
		
		
		String method = req.getParameter("method");
		switch (method) {
		case "selectAll":
			selectAll(req, resp);
			break;
		case "selectByPage":
			selectByPage(req, resp);
			break;
		case "deleteById":
			deleteById(req, resp);
			break;
		case "getUserInsertPage":
			getUserInsertPage(req, resp);
			break;
		case "insert":
			insert(req, resp);
			break;
		case "getUserUpdatePage":
			getUserUpdatePage(req, resp);
			break;
		case "update":
			update(req, resp);
			break;
		case "selectUserDeptInfo":
			selectUserDeptInfo(req, resp);
			break;
		case "login":
			login(req, resp);
			break;
		case "logout":
			logout(req, resp);
			break;
		default:
			System.out.println("default method: " + method);
			break;
		}
	}

	private void logout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		System.out.println("UserServlet.logout()");
		HttpSession session = req.getSession();
		session.removeAttribute("user");
		
		resp.sendRedirect(req.getContextPath() + "/login.jsp");
	}

	private void login(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String checkCode = req.getParameter("checkCode");
		HttpSession session = req.getSession();
		String checkCodeInSession = (String) session.getAttribute("checkCodeInSession");
		if (checkCode.equals(checkCodeInSession) == false) {
			resp.sendRedirect(req.getContextPath() + "/login.jsp");
			return;
		}
		
		String name = req.getParameter("name");
		String password = req.getParameter("password");
		User user = userDao.login(name, password);
		if (user != null) {
			session.setAttribute("user", user);
			resp.sendRedirect(req.getContextPath() + "/");
		} else {
			resp.sendRedirect(req.getContextPath() + "/login.jsp");
		}
	}

	private void selectUserDeptInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("UserServlet.selectUserDeptInfo()");
		List<UserDeptVO> list = userDao.selectUserDeptInfo();
		
		req.setAttribute("list", list);
		req.getRequestDispatcher("/user_dept_list.jsp").forward(req, resp);
	}

	private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// /Java2012WebPersonal/user?method=selectByPage&pageNo=1&pageSize=3
		String pageNoStr = req.getParameter("pageNo");
		String pageSizeStr = req.getParameter("pageSize");
		if (pageNoStr == null || "".equals(pageNoStr)) {
			pageNoStr = "1";
		}
		if (pageSizeStr == null || "".equals(pageSizeStr)) {
			pageSizeStr = "3";
		}
		
		int pageNo = Integer.parseInt(pageNoStr);
		int pageSize = Integer.parseInt(pageSizeStr);
		
		// List<User> list = userDao.selectAll();
		int offset = (pageNo - 1) * pageSize;
		List<User> list = userDao.selectByPage(offset, pageSize);
		
		int totalCount = userDao.selectTotalCount();
		int totalPage = (int)Math.ceil((double)totalCount / pageSize);
		
		PageInfo<User> pageInfo = new PageInfo<User>(list, totalPage, pageNo, pageSize);
		System.out.println(pageInfo);
		
		req.setAttribute("pageInfo", pageInfo);
		req.getRequestDispatcher("/user_list.jsp").forward(req, resp);
		
	}

	private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String name = req.getParameter("name");
		String password = req.getParameter("password");
		String age = req.getParameter("age");
		String email = req.getParameter("email");
		String departmentId = req.getParameter("departmentId");
		User user = new User(Integer.parseInt(id), name, password, Integer.parseInt(age), email, Integer.parseInt(departmentId));
		
		int count = userDao.update(user);
		
		resp.sendRedirect(req.getContextPath() + "/user?method=selectByPage");
	}

	private void getUserUpdatePage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String id = req.getParameter("id");
		User user = userDao.selectById(Integer.parseInt(id));
		
		List<Department> list = departmentDao.selectAll();
		
		req.setAttribute("user", user);
		req.setAttribute("list", list);
		req.getRequestDispatcher("/user_update.jsp").forward(req, resp);
	}

	private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		System.out.println("UserServlet.insert()");
		String name = req.getParameter("name");
		String age = req.getParameter("age");
		String password = req.getParameter("password");
		String email = req.getParameter("email");
		String departmentId = req.getParameter("departmentId");
		User user = new User(name, password, Integer.parseInt(age), email, Integer.parseInt(departmentId));
		
		int count = userDao.insert(user);
		System.out.println("count: " + count);
		
		resp.sendRedirect(req.getContextPath() + "/user?method=selectByPage");
	}

	private void getUserInsertPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("UserServlet.getUserInsertPage()");
		List<Department> list = departmentDao.selectAll();
		for (Department department : list) {
			System.out.println(department);
		}
		
		req.setAttribute("list", list);
		req.getRequestDispatcher("/user_insert.jsp").forward(req, resp);
	}

	private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		System.out.println("UserServlet.deleteById()");
		// http://localhost:8080/Java2012WebPersonal/user?method=deleteById&id=12
		String id = req.getParameter("id");
		int count = userDao.deleteById(Integer.parseInt(id));
		
		// 删除完成之后，服务器要告诉浏览器再去查找所有员工
		resp.sendRedirect(req.getContextPath() + "/user?method=selectByPage");
		
	}

	private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("UserServlet.selectAll()");
		List<User> list = userDao.selectAll();
		
		// 把数据放到request里面
		req.setAttribute("list", list);
		// 转发到user_list.jsp页面展示数据
		req.getRequestDispatcher("/user_list.jsp").forward(req, resp);
	}
	
	
	
}
